package com.hzj.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzj.controller.utils.IPUtils;
import com.hzj.controller.utils.RedisUtil;
import com.hzj.exception.HospitalException;
import com.hzj.helper.HttpRequestHelper;
import com.hzj.pojo.UserInfo;
import com.hzj.pojo.vo.user.LoginVo;
import com.hzj.service.PatientService;
import com.hzj.service.UserInfoService;
import com.hzj.service.UserLoginRecordService;
import com.hzj.utils.Result;
import com.hzj.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class YYLApiUserController {
    @Autowired
    UserInfoService uis;
    @Autowired
    PatientService ps;
    @Autowired
    UserLoginRecordService urs;
    @Autowired
    RedisUtil redisUtil;

    //修改用户邮箱
    @PostMapping("/updateUserMail")
    public Result updateUserMail(@RequestParam String email, @RequestParam String pass, HttpServletRequest request) {
        UserInfo userInfo = (UserInfo) redisUtil.get("userInfo" + IPUtils.getIpAddr(request));
        //验证码
        String userCode = (String) redisUtil.get("userCode|" + email);
        //判断数据库中是否已经存在该邮箱
        QueryWrapper<UserInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",email);
        UserInfo user = uis.getOne(queryWrapper);
         if (!pass.equals(userCode)) {
            return Result.fail();
        } else if (user!=null){
            return Result.build(400,"邮箱已存在");
        } else {
            userInfo.setPhone(email);
            boolean isUpdate = uis.updateById(userInfo);
            //修改redis中的用户信息
            redisUtil.set("userInfo" + IPUtils.getIpAddr(request), userInfo);
            if (!isUpdate) {
                throw new HospitalException(ResultCodeEnum.FAIL);
            }

            return Result.ok();
        }
    }

}
